package megrez.bussiness.system.repository;

import java.text.MessageFormat;

import org.springframework.data.jpa.domain.Specification;

import lombok.experimental.UtilityClass;
import megrez.bussiness.system.entity.Area;

/**
 * 地区查询条件
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@UtilityClass
public class AreaSpecification {
    public Specification<Area> nameLike(String name) {
        return (root, query, builder) -> builder.like(root.get("name"), contains(name));
    }

    public Specification<Area> parentIsNull() {
        return (root, query, builder) -> builder.isNull(root.get("parent"));
    }

    private String contains(String expression) {
        return MessageFormat.format("%{0}%", expression);
    }
}
